package sdkshell.kuaiyan.com.kuaiyansdk;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.text.TextUtils;

import com.wmcsk.KuaiyanSdk;
import com.wmcsk.dl.util.LogUtils;

import sdkshell.kuaiyan.com.kuaiyansdk.manager.NetWorkPluginManager;


/**
 * Created by ${刘红亮} on 15-7-12 下午5:55.
 */
public class PluginService extends Service {
    public static final String Action = "com.kuaiyan.plugin.check";
    public UpdateBroadcastReciver broadcastReceiver = new UpdateBroadcastReciver() ;
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Action);
        registerReceiver(broadcastReceiver,intentFilter);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return Service.START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if(broadcastReceiver!=null){
            unregisterReceiver(broadcastReceiver);
        }
    }

    public class UpdateBroadcastReciver extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
            LogUtils.getNewInstance(KuaiyanSdk.ERROR_TAG).eob("begin check update");
            String action = intent.getAction();
            if(Action.equals(action)){
                String name = intent.getStringExtra("apkName");
                LogUtils.getNewInstance(KuaiyanSdk.ERROR_TAG).eob("begin check update plugin:"+name);
                if(!TextUtils.isEmpty(name)){
                    NetWorkPluginManager.getInstance(SdkInit.getContext()).checkUpdate(name);
                }
            }
        }
    }
}
